package com.softroad.help.app.ui;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.softroad.help.app.AppManager;
import com.softroad.help.app.R;
import com.softroad.help.app.bean.ReportBean;
import com.softroad.help.app.tools.DialogUtils;
import com.softroad.help.app.tools.FileUtil;
import com.softroad.help.app.tools.StringTool;
import com.softroad.help.app.tools.ToastTool;

public class ReportActivity extends BaseActivity implements OnClickListener {
	private ImageView mine_head;
	private ImageView head_img;
	private EditText titlEditText;
	private EditText contentEditText;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;
	private ReportBean bean;

	private File cameraFile;
	private File cropFile;
	private String base64 = "";
	private Bitmap bitmap;

	public static void startActivity(Context context, ReportBean bean) {
		context.startActivity(new Intent(context, ReportActivity.class)
				.putExtra("bean", bean));
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.report;
	}

	@Override
	public void initView(View view) {
		cropFile = new File(Environment.getExternalStorageDirectory(), "jpg");
		bean = (ReportBean) getIntent().getSerializableExtra("bean");
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.defaults)
				.showImageOnFail(R.drawable.defaults).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		action_title.setText(R.string.tab2);
		head_img = $(R.id.head_img);
		mine_head = $(R.id.mine_head);
		titlEditText = $(R.id.title);
		contentEditText = $(R.id.content);
	}

	@Override
	public void initData(Context mContext) {
		if (bean == null) {
			AppManager.getAppManager().finishActivity();
		}
		titlEditText.setText(bean.getStrTitle().toString().trim());
		contentEditText.setText(bean.getStrContent().toString().trim());
		String imageUrl = bean.getImageBase64();
		if (StringTool.isNoBlankAndNoNull(imageUrl)) {
			imageLoader.displayImage(imageUrl, head_img, options,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingStarted(String imageUri, View view) {
						}

						@Override
						public void onLoadingFailed(String imageUri, View view,
								FailReason failReason) {
						}

						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
						}
					}, null);
		}
	}

	@Override
	public void initListener() {
		mine_head.setOnClickListener(this);
		action_back.setOnClickListener(this);
	}

	@Override
	public void resume() {

	}

	@Override
	public void destroy() {
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 拍照
		if (requestCode == DialogUtils.REQUEST_CODE_CAMERA
				&& FileUtil.isValid(cameraFile) && resultCode == RESULT_OK) {
			DialogUtils.startPhotoCrop(this, Uri.fromFile(cameraFile), 80,
					cropFile);
			cameraFile = null;
			// 系统图片库-选择图片
		} else if (requestCode == DialogUtils.REQUEST_CODE_IMAGE
				&& data != null && resultCode == RESULT_OK) {
			DialogUtils.startPhotoCrop(this, data.getData(), 80, cropFile);
			// 裁剪图片
		} else if (requestCode == DialogUtils.REQUEST_CODE_CROP && data != null
				&& resultCode == RESULT_OK) {
			bitmap = BitmapFactory.decodeFile(cropFile.getAbsolutePath());
			if (bitmap != null) {
				// 设置妈妈头像
				mine_head.setImageBitmap(bitmap);
				base64 = DialogUtils.imgToBase64(bitmap);

			} else {
				ToastTool.toastMessage(this, "用户存储空间不足，请整理空间后上传!");
			}
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.action_back:
			AppManager.getAppManager().finishActivity();
			break;

		case R.id.mine_head:
			cameraFile = DialogUtils.showImageDialog(this);
			break;
		default:
			break;
		}
	}

}
